package com.cn.goodsadmin.controller;


import com.cn.goodsadmin.po.Vip;
import com.cn.goodsadmin.service.VipService;
import com.cn.goodsadmin.util.ErrorMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@CrossOrigin(origins = {"http://10.10.10.143:8020"}, maxAge = 3600)
@CrossOrigin(origins = "*", maxAge = 3600)
@Controller
public class VipController {

    @Autowired
    VipService vipService;

    @LoginRequired
    @RequestMapping(value = "/getallvip", method = RequestMethod.GET,params = {"appid"})
    @ResponseBody
    public SupplyMap getallVip(int appid) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            List<Vip> vips = vipService.getAllVip(appid);
            if (vips != null) {
                supplyMap.put("vips", vips);
            }
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }

    @LoginRequired
    @RequestMapping(value = "/addvip", method = RequestMethod.POST, produces = "application/json;charset=utf-8", consumes = "application/json;charset=utf-8")
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap addVip(@RequestBody Vip vip) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            vipService.save(vip);
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }


    //@LoginRequired
    @RequestMapping(value = "/updatevip", method = RequestMethod.POST, produces = "application/json;charset=utf-8", consumes = "application/json;charset=utf-8")
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap updateVip(@RequestBody Vip vip) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            vipService.update(vip);
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }


    //@LoginRequired
    @RequestMapping(value = "/deletevip", method = RequestMethod.POST, produces = "application/json;charset=utf-8", consumes = "application/json;charset=utf-8")
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap deleteVip(@RequestBody Vip vip) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            vipService.delete(vip);
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }
}
